package com.golddaymicromall.activity.eleaccounts;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.golddaymicromall.activity.R;
import com.golddaymicromall.application.MyApplication;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.util.AgentUtils;
import com.golddaymicromall.util.ButtonUtils;
import com.golddaymicromall.util.MD5;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import rx.android.schedulers.AndroidSchedulers;

public class ChangeDianZiActivity extends BaseActivity implements View.OnClickListener {
    private TextView mTv_change;
    private String customerid;
    private String Et_change;
    private EditText mEt_change;
    private String walletid;
    private LinearLayout mactivity_zhuanwejinhuodianzibi_btn_back;
    private String Customerno;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_change_dian_zi);
        Customerno = SPUtils.get(getApplicationContext(), "Customerno", "").toString();
        customerid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        walletid = getIntent().getStringExtra("walletid");
        initView();

    }

    private void initView() {
        mactivity_zhuanwejinhuodianzibi_btn_back = (LinearLayout) findViewById(R.id.activity_zhuanwejinhuodianzibi_btn_back);
        mactivity_zhuanwejinhuodianzibi_btn_back.setOnClickListener(this);
        mTv_change = (TextView) findViewById(R.id.Tv_change1);
        mEt_change = (EditText) findViewById(R.id.Et_change);
        mEt_change.setSelection(mEt_change.getText().length());
        mTv_change.setText(Customerno);

        Button mSignInButton = (Button) findViewById(R.id.btnChange);
        mSignInButton.setOnClickListener(view -> {
            if (ButtonUtils.isFastDoubleClick()) {
                return;
            }
            Et_change = mEt_change.getText().toString().trim();

            if (AgentUtils.isBlank(Et_change)) {
                Toast.makeText(getApplicationContext(), "请输入金额", Toast.LENGTH_SHORT).show();
                return;
            }
            if (AgentUtils.isBlank(Et_change)) {
                Toast.makeText(getApplicationContext(), "请输入金额", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!isMoney(Et_change)) {
                Toast.makeText(getApplicationContext(), "请输入正确的金额格式", Toast.LENGTH_SHORT).show();
                return;
            }
            if (Double.parseDouble(Et_change) < 0.01) {
                Toast.makeText(getApplicationContext(), "支出金额不能小于0.01元", Toast.LENGTH_SHORT).show();
                return;
            }

            attemptChangeDianZi(customerid,Et_change,walletid);

        });
    }



    public boolean isMoney(String Num) {
        Pattern p = Pattern
                .compile("^(([1-9]\\d{0,9})|0)(\\.\\d{1,2})?$");
        Matcher m = p.matcher(Num);
        return m.matches();
    }
    public boolean isSpecial(String bz) {
        String regEx = "[`~!@#$%^&*()+=|{}':;'\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(bz);
        return m.find();
    }

    private void attemptChangeDianZi(String customerid,String et,String walletid) {

        //获取本地数据
        MD5 md = new MD5();
        String code = md.toMD5(et);
        changeToJinhuo(customerid, code, et, walletid);

    }
    private void changeToJinhuo(String customerid, String code, String edt, String wd) {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.profit.out");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerid", customerid);
        form.put("walletid", wd);
        form.put("memo", "A:");
        form.put("outmoney", edt);
        form.put("outmoney2", code);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            if (result.equals("1")) {
                                Toast.makeText(getApplicationContext(), "转换成功", Toast.LENGTH_SHORT).show();
                                finish();
                                MyApplication.getInstance().attemptRiZhi(Customerno, "收益账户转到进货账户电子币" + edt);
                            }else if(result.equals("-2")){
                                Toast.makeText(getApplicationContext(), "暂时无法转为电子币，请联系客服", Toast.LENGTH_SHORT).show();
                            } else if(result.equals("-1")){
                                Toast.makeText(getApplicationContext(), "余额不足", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(getApplicationContext(), "转换失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    showError();
                    throwable.printStackTrace();
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_zhuanwejinhuodianzibi_btn_back:
                finish();
                break;
        }
    }



}
